﻿@{
    ViewData["Title"] = "AddView";
    Layout = null;
}

<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>AddView</title>
    <link href="~/libs/layui-v2.5.6/layui/css/layui.css" rel="stylesheet" />
</head>
<body>

    <div class="layui-form" style="max-width: 800px; margin: 0 auto;">
        <div class="layui-form-item">
            <label class="layui-form-label">* 通知单编号</label>
            <div class="layui-input-inline">
                <input type="text" name="notificationCode" id="notificationCode" required lay-verify="required" value="AN20250114002" class="layui-input">
                <button type="button" class="layui-btn layui-btn-primary" id="generateCode">生成编号</button>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">采购订单编号</label>
            <div class="layui-input-inline">
                <input type="text" name="purchaseOrderCode" id="purchaseOrderCode" placeholder="请输入采购订单编号" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">* 通知单名称</label>
            <div class="layui-input-inline">
                <input type="text" name="notificationName" id="notificationName" required lay-verify="required" class="layui-input" placeholder="请输入通知单名称">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">供应商</label>
            <div class="layui-input-inline">
                <select name="supplier" id="supplier" lay-filter="supplier" class="layui-input">
                    <option value="">请选择供应商</option>
                    <!-- 插入供应商选项 -->
                </select>
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">* 到货日期</label>
            <div class="layui-input-inline">
                <input type="text" name="arrivalDate" id="arrivalDate" lay-verify="date" placeholder="yyyy-MM-dd" autocomplete="off" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系人</label>
            <div class="layui-input-inline">
                <input type="text" name="contactPerson" id="contactPerson" placeholder="请输入联系人" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联系方式</label>
            <div class="layui-input-inline">
                <input type="text" name="contactInformation" id="contactInformation" placeholder="请输入联系方式" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">联络方式</label>
            <div class="layui-input-inline">
                <input type="text" name="contactMethod" id="contactMethod" placeholder="请输入联络方式" class="layui-input">
            </div>
        </div>

        <div class="layui-form-item">
            <label class="layui-form-label">备注</label>
            <div class="layui-input-block">
                <textarea name="remark" id="remark" rows="4" placeholder="请输入内容" class="layui-textarea"></textarea>
            </div>
        </div>

        <div style="display: flex; justify-content: space-between; margin-top: 20px;">
            <button type="button" class="layui-btn layui-btn-normal" onclick="addCreate()">保存</button>
            <button type="button" class="layui-btn layui-btn-primary" onclick="window.close()">取消</button>
        </div>
    </div>


    <!-- 确保 jQuery 在 Layui 之前加载 -->
    <script src="~/libs/jquery/jquery.js"></script>
    <script src="~/libs/layui-v2.5.6/layui/layui.js"></script>

    <script>
        $(function() {
            // 获取供应商数据
            GetVendor();
        });

        // 确保在layui.use的回调函数中定义所有的layui相关逻辑
        layui.use(['form', 'layer', 'laydate'], function() {
            var form = layui.form;
            var layer = layui.layer;
            var laydate = layui.laydate;

            // 日期选择器
            laydate.render({
                elem: '#date'
            });


        });


        //新增
        function addCreate(){

            //供应商非空
            if($("#arrivalDates").val()==="0"){
                layer.msg('请选择供应商');
                return;
            }

            //手机号验证
            if (!/^1[3-9]\d{9}$/.test($("#contactInformation").val())) {
                layer.msg('请输入正规手机号');
                return ;
            }

            var data = {
                
                notificationCode: $("#notificationCode").val(),
                notificationName: $("#purchaseOrderCode").val(),
                purchaseOrderCode: $("#notificationName").val(),
                arrivalDates: $("#arrivalDates").val(),
                arrivalDate: $("#date").val().toString(),
                contactPerson: $("#contactPerson").val(),
                contactInformation: $("#contactInformation").val(),
                remark: $("#remark").val()
            };

            console.log("收集到的数据:", JSON.stringify(data));


            $.ajax({
                url: '/NoticeOfArrival/AddCreate',
                type: 'post',
                data: data,
                dataType:'json',
                async:false,
                success: function(response) {
                    if(response.code>0){
                       layer.msg('新增成功！');
                       reset();
                    }
                }
            });

        }


        //获取供应商信息
        function GetVendor() {
            $.ajax({
                url: '/NoticeOfArrival/GetVendor',
                type: 'get',
                dataType: 'json',
                async: true,
                success: function(res) {
                    console.log(res);
                    var html = '<option value="0">请选择</option>';
                    $.each(res.data, function(i, item) {
                        html += '<option value="' + item.name + '">' + item.name + '</option>';
                    });
                    $('#supplier').html(html);
                    layui.form.render('select'); 

                    window.vendorData = res.data;

                    // 监听下拉框的变化
                    layui.form.on('select(supplier)', function(data) {
                        console.log("下拉框变化:", data.value); // 输出选中的值

                        // 查找对应的数据
                        var selectedItem = window.vendorData.find(x => x.name === data.value);
                        if (selectedItem) {
                            $('#contactPerson').val(selectedItem.contac || '');
                            $('#contactInformation').val(selectedItem.vendorPhone || ''); 
                        }
                    });
                }
            });
        }

        // 生成通知单编号
        document.getElementById('generateCode').addEventListener('click', function() {
            var prefix = 'AN';
            var date = new Date();
            var year = date.getFullYear().toString().slice(-2); // 获取最后两位年份
            var month = ('0' + (date.getMonth() + 1)).slice(-2); // 获取月份并补零
            var day = ('0' + date.getDate()).slice(-2); // 获取日期并补零

            // 创建新的通知单编号
            var newCode = prefix + year + month + day + Math.floor(1000 + Math.random() * 9000);
            document.getElementById('notificationCode').value = newCode;
        });

        // 返回原页面
        function reset() {
            console.log("返回");
            // 在当前iframe => 关闭当前iframe
            var index = parent.layer.getFrameIndex(window.name); // 先得到当前iframe层的索引
            parent.layer.close(index); // 再执行关闭
        }
    </script>

</body>
</html>